package com.freedom.mysql.myrwsplit.helper;

import java.util.ArrayList;

public class UrlHelper {
	private static LoggerHelper LOGGER = LoggerHelper.getLogger(UrlHelper.class);

	public static void main(String[] args) throws Exception {
		String url = "jdbc:mysql://{106.1.1.1:3306,106.1.1.1:3306,106.1.1.1:3306}/ambari?zeroDateTimeBehavior=convertToNull";
		System.out.println(handle(url));
	}

	public static ArrayList<String> handle(String url) throws Exception {
		ArrayList<String> result = new ArrayList<String>();
		//
		LOGGER.debug("url ---> " + url);
		if (false == url.startsWith("jdbc:mysql://{")) {
			throw new Exception("not legal mysql url,must start with jdbc:mysql://{");
		}
		int beginIndex = url.indexOf("{");
		if (-1 == beginIndex) {
			throw new Exception("not legal mysql url,no { found");
		}
		int endIndex = url.indexOf("}");
		if (-1 == endIndex) {
			throw new Exception("not legal mysql url,no } found");
		}
		String urlPrefix = url.substring(0, beginIndex);
		String ipPorts = url.substring(beginIndex + 1, endIndex);
		String urlSuffix = url.substring(endIndex + 1);
		LOGGER.debug("urlPrefix--->" + urlPrefix);
		LOGGER.debug("ipPorts--->" + ipPorts);
		LOGGER.debug("urlSuffix--->" + urlSuffix);
		//
		String[] array = ipPorts.split(",");
		if (null == array || array.length <= 1) {
			throw new Exception("not legal mysql url,at least 2 ip:port");
		}
		for (String ipPort : array) {
			result.add(urlPrefix + ipPort + urlSuffix);
		}
		return result;
	}
}
